<?php
/**
 * 消费管理控制器
 * create by liuqi
 */
namespace cash\Controller;
use Think\Controller;

class ConsumeController extends ApibaseController {
	/**
	 * 套餐消费列表，财务端用
	 */
	public function consumeList(){
		$page = I('get.page',1);
		$perpage = 10;
		$stime = I('get.stime');
		$etime = I('get.etime');
		$url = [];
		$list = M('OrderEntity oe')
				->join('pf_project_entity pe ON oe.project_id = pe.project_id','LEFT')
				->field('oe.id,oe.order_id,oe.user_id,oe.user_name,oe.order_amount,oe.user_mobile,oe.is_used,oe.score_type,oe.consumer,oe.consumer_mobile,oe.class_room,oe.servicer,oe.counselor,oe.operate_nurse,oe.operate_place,oe.add_at,oe.use_at,pe.project_name,pe.cover_img,pe.project_id')
				->where(['oe.is_used'=>1]);

		if(in_array(I('get.score_type'),[1,2,3,4,5,6])){
			$list = $list->where(['oe.score_type'=>I('get.score_type')]);
			$url['score_type'] = trim(I('get.score_type'));
		}
		if(trim(I('get.user_mobile'))){
			$list = $list->where(['oe.user_mobile'=>trim(I('get.user_mobile'))]);
			$url['user_mobile'] = trim(I('get.user_mobile'));
		}
		if(trim(I('get.user_name'))){
			$list = $list->where("user_name LIKE '%".trim(I('get.user_name'))."%'");
			$url['user_name'] = trim(I('get.user_name'));
		}
		if(trim(I('get.order_id'))){
			$list = $list->where(['oe.order_id'=>trim(I('get.order_id'))]);
			$url['order_id'] = trim(I('get.order_id'));
		}
		if ($stime && !$etime) {
			$list         = $list->where(['oe.use_at' => ['gt', strtotime($stime)]]);
			$url['stime'] = $stime;
		} elseif (!$stime && $etime) {
			$list         = $list->where(['oe.use_at' => ['lt', strtotime($etime)]]);
			$url['etime'] = $etime;
		} elseif ($stime && $etime) {
			$list         = $list->where(['oe.use_at' => ['between', [strtotime($stime), strtotime($etime) + 86400]]]);
			$url['stime'] = $stime;
			$url['etime'] = $etime;
		}
		$getObj = clone $list;
		$getData = $list->order(['oe.add_at'=>'desc'])->page($page,$perpage)->select();
		$this->assign('page',setPage($getObj->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('getData',$getData);
		$this->display('consume/list');
	}


	/**
	 * 前端注册用户列表
	 */
	public function userList()
	{
		$page = I('get.page',1);
		$perpage = I('get.perpage',10);
		$url = [];
		$model = M('UserInfo');

		if(trim(I('get.user_name'))){
			$model = $model->where(['user_name'=>trim(I('get.user_name'))]);
			$url['user_name'] = I('get.user_name');
		}
		if(trim(I('get.user_mobile'))){
			$model = $model->where(['user_mobile'=>trim(I('get.user_mobile'))]);
			$url['user_mobile'] = I('get.user_mobile');
		}
		$copy = clone $model;
		$getData = $model->order(['add_at' => 'desc'])->page($page, $perpage)->select();
		$this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
		$this->assign('getData', $getData);
		$this->display('consume/user_list');
	}

	public function setStatus()
	{
		if(I('get.types')){
			M('UserInfo')->where(array('user_id'=>I('get.uid')))->setField(array('status' => '1'));
			$this->showtips('成功解封该用户');
		}
		M('UserInfo')->where(array('user_id'=>I('get.uid')))->setField(array('status' => '0'));
		$this->showtips('成功冻结该用户');
	}

	/**
	 * 套餐消费列表 导出表格
	 * create by: liuqi
	 * Date: 2018-2-2 13:56:18
	 */
	public function exportUsedPro()
	{
		set_time_limit(0);
		$stime = I('get.stime');
		$etime = I('get.etime');
		$list = M('OrderEntity oe')
			->join('pf_project_entity pe ON oe.project_id = pe.project_id','LEFT')
			->field('oe.id,oe.order_id,oe.user_id,oe.score_type,oe.consumer,oe.consumer_mobile,oe.use_at,oe.order_amount,oe.class_room,oe.servicer,oe.counselor,oe.operate_nurse,oe.operate_place,pe.project_name,pe.cover_img')
			->where(['oe.is_used'=>1]);

		if(in_array(I('get.score_type'),[1,2,3,4,5,6])){
			$list = $list->where(['oe.score_type'=>I('get.score_type')]);
			$url['score_type'] = trim(I('get.score_type'));
		}
		if(trim(I('get.user_mobile'))){
			$list = $list->where(['oe.user_mobile'=>trim(I('get.user_mobile'))]);
			$url['user_mobile'] = trim(I('get.user_mobile'));
		}
		if(trim(I('get.user_name'))){
			$list = $list->where("user_name LIKE '%".trim(I('get.user_name'))."%'");
			$url['user_name'] = trim(I('get.user_name'));
		}
		if(trim(I('get.order_id'))){
			$list = $list->where(['oe.order_id'=>trim(I('get.order_id'))]);
			$url['order_id'] = trim(I('get.order_id'));
		}
		if ($stime && !$etime) {
			$list         = $list->where(['oe.use_at' => ['gt', strtotime($stime)]]);
			$url['stime'] = $stime;
		} elseif (!$stime && $etime) {
			$list         = $list->where(['oe.use_at' => ['lt', strtotime($etime)]]);
			$url['etime'] = $etime;
		} elseif ($stime && $etime) {
			$list         = $list->where(['oe.use_at' => ['between', [strtotime($stime), strtotime($etime) + 86400]]]);
			$url['stime'] = $stime;
			$url['etime'] = $etime;
		}
		$getData = $list->order(['oe.add_at'=>'desc'])->select();
		$tableHeader = ['订单号','套餐名称','类型','消费积分','消费人','消费人手机号','课室','服务商','咨询师','操作地点','消费时间'];
		$data = [];
		$score_type = ['','普通积分','白金积分','钻石积分','体验积分','黄金积分','口腔积分'];
		if($getData){
			foreach($getData as $k=>$v){
				$data[] = [
					'0'	=> $v['order_id'],
					'1'	=> $v['project_name'],
					'2'	=> $score_type[$v['score_type']],
					'3'	=> $v['order_amount'],
					'4'	=> $v['consumer'],
					'5'	=> $v['consumer_mobile'],
					'6'	=> $v['class_room'],
					'7'	=> $v['servicer'],
					'8'	=> $v['counselor'],
					'9'=> $v['operate_place'],
					'10'=> date('Y-m-d',$v['use_at']),
				];
			}
		}

		if($data){
			exportExcel($tableHeader,$data);
		}else{
			$this->showtips('没有数据导出失败');
		}

	}

	/**
	 * 兑换列表
	 * create by: liuqi
	 * Date:2017-12-25 16:51:25
	 */
	public function exchange()
	{
		$page = I('get.page',1);
		$perpage = 10;
		$url = [];

		$list = M('ExchangeEntity');
		if(trim(I('get.user_mobile'))){
			$list = $list->where(['user_mobile'=>trim(I('get.user_mobile'))]);
			$url['user_mobile'] = trim(I('get.user_mobile'));
		}
		$copy = clone $list;
		$getData = $list->order(['add_at'=>'desc'])->page($page,$perpage)->select();
		foreach($getData as $k=>$v){
			$getData[$k]['user_name'] = M('UserInfo')->where(['user_id'=>$v['user_id']])->getField('user_name');
		}
		$this->assign('page',setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
		$this->assign('getData',$getData);
		$this->display('consume/exchange_list');
	}

    /**
     * 查看套餐的具体详情
     * create by: liuqi
     * Date:2017-12-27 11:49:28
     */
    public function getProjectDetail()
    {
        if (I('get.order_id')) {
            $data = M('CardEntity')->where(['order_id' => I('get.order_id')])->select();
            $this->assign('getData', $data);
            $this->display('consume/project_detail');
        }
    }

	/***
	 * 项目消耗列表
	 * create by: liuqi
	 * Date:2018-2-2 13:46:41
	 */
	public function proConsumeList()
	{
		$page = I('get.page',1);
		$perpage = 10;
		$stime = I('get.stime');
		$etime = I('get.etime');
		$url = [];
		$list = M('CardEntity ce')
				->join('pf_order_entity oe ON ce.order_id = oe.order_id','LEFT')
				->field('ce.card_id,ce.project_id,ce.order_id,ce.goods_name,ce.operate_nurse,ce.operate_doctor,ce.operate_place,ce.use_at,oe.consumer,oe.consumer_mobile,oe.user_name,oe.user_mobile')
				->where(['ce.is_used'=>1]);

		if(trim(I('get.order_id'))){
			$list = $list->where(['ce.order_id'=>trim(I('get.order_id'))]);
			$url['order_id'] = trim(I('get.order_id'));
		}
		if(trim(I('get.card_name'))){
			$list = $list->where("ce.goods_name LIKE '%".trim(I('get.card_name'))."%'");
			$url['card_name'] = trim(I('get.card_name'));
		}
		if(trim(I('get.user_mobile'))){
			$list = $list->where(['oe.user_mobile'=>trim(I('get.user_mobile'))]);
			$url['user_mobile'] = trim(I('get.user_mobile'));
		}
		if(trim(I('get.user_name'))){
			$list = $list->where("user_name LIKE '%".trim(I('get.user_name'))."%'");
			$url['user_name'] = trim(I('get.user_name'));
		}

		if ($stime && !$etime) {
			$list         = $list->where(['ce.use_at' => ['gt', strtotime($stime)]]);
			$url['stime'] = $stime;
		} elseif (!$stime && $etime) {
			$list         = $list->where(['ce.use_at' => ['lt', strtotime($etime)]]);
			$url['etime'] = $etime;
		} elseif ($stime && $etime) {
			$list         = $list->where(['ce.use_at' => ['between', [strtotime($stime), strtotime($etime) + 86400]]]);
			$url['stime'] = $stime;
			$url['etime'] = $etime;
		}
		$project_info = M('ProjectEntity')->where(['delete_at'=>0])->field('project_id,project_name,score_type,project_price')->select();
		foreach($project_info as $k=>$v){
			$new_pro_info[$v['project_id']]['project_name'] = $v['project_name'];
			$new_pro_info[$v['project_id']]['score_type'] = $v['score_type'];
			$new_pro_info[$v['project_id']]['project_price'] = $v['project_price'];
		}
		$getObj = clone $list;
		$getData = $list->order(['ce.use_at'=>'desc'])->page($page,$perpage)->select();
		foreach($getData as $k1=>$v1){
			$getData[$k1]['project_name'] = $new_pro_info[$v1['project_id']]['project_name'];
			$getData[$k1]['score_type'] = $new_pro_info[$v1['project_id']]['score_type'];
			$getData[$k1]['project_price'] = $new_pro_info[$v1['project_id']]['project_price'];
		}
		$this->assign('page',setPage($getObj->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('getData',$getData);
		$this->display('consume/pro_con_list');
	}

	/***
	 * 项目消费列表导出
	 * create by: liuqi
	 * Date:2018-1-27 11:38:21
	 */
	public function exportUsedCard()
	{
		$stime = I('get.stime');
		$etime = I('get.etime');
		set_time_limit(0);
		$list = M('CardEntity ce')->join('pf_order_entity oe ON ce.order_id = oe.order_id','LEFT')->field('ce.project_id,ce.order_id,ce.goods_name,ce.operate_nurse,ce.operate_doctor,ce.operate_place,ce.use_at,oe.consumer,oe.consumer_mobile')->where(['ce.is_used'=>1]);

		if(trim(I('get.order_id'))){
			$list = $list->where(['ce.order_id'=>trim(I('get.order_id'))]);
			$url['order_id'] = trim(I('get.order_id'));
		}
		if(trim(I('get.card_name'))){
			$list = $list->where("ce.goods_name LIKE '%".trim(I('get.card_name'))."%'");
			$url['card_name'] = trim(I('get.card_name'));
		}
		if(trim(I('get.user_mobile'))){
			$list = $list->where(['oe.user_mobile'=>trim(I('get.user_mobile'))]);
			$url['user_mobile'] = trim(I('get.user_mobile'));
		}
		if(trim(I('get.user_name'))){
			$list = $list->where("user_name LIKE '%".trim(I('get.user_name'))."%'");
			$url['user_name'] = trim(I('get.user_name'));
		}

		if ($stime && !$etime) {
			$list         = $list->where(['ce.use_at' => ['gt', strtotime($stime)]]);
			$url['stime'] = $stime;
		} elseif (!$stime && $etime) {
			$list         = $list->where(['ce.use_at' => ['lt', strtotime($etime)]]);
			$url['etime'] = $etime;
		} elseif ($stime && $etime) {
			$list         = $list->where(['ce.use_at' => ['between', [strtotime($stime), strtotime($etime) + 86400]]]);
			$url['stime'] = $stime;
			$url['etime'] = $etime;
		}
		$project_info = M('ProjectEntity')->where(['delete_at'=>0])->field('project_id,project_name,score_type,project_price')->select();
		foreach($project_info as $k=>$v){
			$new_pro_info[$v['project_id']]['project_name'] = $v['project_name'];
			$new_pro_info[$v['project_id']]['score_type'] = $v['score_type'];
			$new_pro_info[$v['project_id']]['project_price'] = $v['project_price'];
		}
		$getData = $list->order(['ce.add_at'=>'desc'])->select();
		$type = ['','普通积分','白金积分','钻石积分','体验积分','黄金积分'];
		foreach($getData as $k1=>$v1){
			$getData[$k1]['project_name'] = $new_pro_info[$v1['project_id']]['project_name'];
			$getData[$k1]['score_type'] = $new_pro_info[$v1['project_id']]['score_type'];
			$getData[$k1]['project_price'] = $new_pro_info[$v1['project_id']]['project_price'];
		}
		$tableHeader = ['订单号','套餐名称','类型','消费积分','消费人','消费人手机号','套餐内容','操作护士','操作医生','操作地点','消费时间'];
		if($getData){
			foreach($getData as $k=>$v){
				$data[] = [
					'0'	=> $v['order_id'],
					'1'	=> $v['project_name'],
					'2'	=> $type[$v['score_type']],
					'3'	=> $v['project_price'],
					'4'	=> $v['consumer'],
					'5'	=> $v['consumer_mobile'],
					'6'	=> $v['goods_name'],
					'7'	=> $v['operate_nurse'],
					'8' => $v['operate_doctor'],
					'9' => $v['operate_place'],
					'10' => date('Y-m-d',$v['use_at']),
				];
			}
		}

		if($data){
			exportExcel($tableHeader,$data);
		}else{
			$this->showtips('没有数据导出失败');
		}

	}

	/***
	 * 编辑项目内容 只允许修改 操作护士 医生 地点
	 * create by: liuqi
	 * Date:2018-1-29 15:09:15
	 */
	public function editProConsume()
	{
		if(I('post.dosubmit')){
			if(I('post.card_id')){
				$data = [
					'operate_nurse'=>trim(I('post.operate_nurse')),
					'operate_doctor'=>trim(I('post.operate_doctor')),
					'operate_place'=>trim(I('post.operate_place')),
					'update_at'	=> time()
				];
				$res = M('CardEntity')->where(['card_id'=>intval(I('post.card_id'))])->setField($data);
				if($res){
					$this->showtips('修改成功');
				}else{
					$this->showtips('修改失败');
				}
			}
		}elseif(I('get.card_id')){
			$getData = M('CardEntity')->where(['card_id'=>intval(I('get.card_id'))])->find();
			$this->assign('getData',$getData);
			$this->display('consume/edit_con');
		}
	}

	/***
	 * 获取用户积分名字列表
	 * create by: liuqi
	 * Date:2018年5月11日13:33:34
	 */

	public function getUsedDetail()
	{
		$user_id = intval(I('get.user_id'));
		$score_type = intval(I('get.score_type'));
		if($user_id && $score_type){
			$data_list = M('ScoreDetail')->where(['user_id'=>$user_id,'detail_type'=>2,'score_type'=>$score_type])->select();
			$this->assign('data',$data_list);
			$this->display('consume/used_score_detail');
		}
	}

}